﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bitirme.Controllers;
using Bitirme.DAL.Entities;

namespace Bitirme.Views
{
    public partial class AddWorkPackage : System.Web.UI.Page
    {

        Project project;
        WorkPackageHandler workPackageHandler;


        protected void Page_Load(object sender, EventArgs e)
        {

                string iD = Request.QueryString["id"];
                int projectid = Int32.Parse(iD);
                ProjectHandler projectHandler = new ProjectHandler();
                workPackageHandler = new WorkPackageHandler();
                project = projectHandler.SelectProjectByID(projectid);
                ProjectName.Text = project.Name;
                ProjectSubject.Text = project.Subject;
                ProjectDetail.Text = project.Details;
                ProjectCost.Text = project.Cost.ToString();
                ProjectDuration.Text = project.Duration.ToString();
            if (!Page.IsPostBack)
            {
                CheckBoxList1.DataSource = workPackageHandler.GetAllWorkPackagesByProjectID(projectid);
                CheckBoxList1.DataTextField = "Name";
                CheckBoxList1.DataValueField = "ID";
                CheckBoxList1.DataBind();
            }
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = NameTextBox.Text;
            string subject = SubjectTextBox.Text;
            string requirement = CapabilityTextBox.Text;
            int cost = Convert.ToInt32(CostTextBox.Text);
            int manhour = Convert.ToInt32(ManHourTextBox.Text);
            int workPackageState = Convert.ToInt32(WorkPackageStateList.SelectedItem.Value);

            List<WorkPackage> dependencyList = new List<WorkPackage>(); 
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    WorkPackage tempWorkPackage = new WorkPackage();
                    tempWorkPackage.ID =  Convert.ToInt32(CheckBoxList1.Items[i].Value);
                    tempWorkPackage.Name = CheckBoxList1.Items[i].Text;
                    dependencyList.Add(tempWorkPackage);
                }
            }

            int intResult;
            try
            {

                intResult = workPackageHandler.InsertWorkPackage(name, subject, requirement, manhour, cost, workPackageState, project,dependencyList);

                if (intResult > 0)

                    ResultMessage.Text = "İş Paketi başarıyla eklendi. :" + intResult;

                else

                    ResultMessage.Text = "Hata: İş Paketi Eklenemedi!";

            }

            catch (Exception ee)
            {

                ResultMessage.Text = ee.Message.ToString();

            }

            finally
            {

                workPackageHandler = null;

            }    

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}